package cn.rayland.androidkeyevent;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private PowerKeyEventReceiver receiver = new PowerKeyEventReceiver();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //实现回调方法
        receiver.setOnPowerKeyListener(new PowerKeyEventReceiver.OnPowerKeyListener() {
            @Override
            public void onPowerKeyClick() {
                Log.d(TAG, "onPowerKeyClick");
            }

            @Override
            public void onPowerKeyDoubleClick() {
                Log.d(TAG, "onPowerKeyDoubleClick");
            }

            @Override
            public void onPowerKeyLongPressed() {
                Log.d(TAG, "onPowerKeyLongPressed");
            }
        });

        //注册接收器
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
        intentFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        registerReceiver(receiver, intentFilter);
    }


    @Override
    protected void onDestroy() {
        //注销接收器
       unregisterReceiver(receiver);
        super.onDestroy();
    }
}
